//Initialize the file system
#include <cstdio>
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include "head/superblock.h"
#include "head/inode.h"
#include "head/command.h"
using namespace std;

enum class Instr {CREAT_FILE=1, DELETE_FILE, CREATE_DIR, DELETE_DIR, CHANGE_DIR, DIR, CP, SUM, CAT, WRITE};
map<string, Instr> instruction;
const SuperBlock* superblock = new SuperBlock;
Inode* root = new Inode(0, Mode::RW, Type::DIR);
Inode* stand = new Inode;
Inode* temp = new Inode;

char inode_bit_map[1024];
char data_bit_map[1024];
FILE* fp;

// function
void initInstrucationMap(); 
void dealCommand(string, string&);//receive command and invoke corresponding function


int main() {
    inode_bit_map[0]=1;
    fp = fopen("fss", "rb+");
    fseek(fp, superblock->inode_base, SEEK_SET);
    fread(root, sizeof(Inode), 1, fp);

    fseek(fp, superblock->inode_bitmap_base, SEEK_SET);
    fread(inode_bit_map, 1, 1024, fp);

    fseek(fp, superblock->data_bitmap_base, SEEK_SET);
    fread(data_bit_map, 1, 1024, fp);
    memcpy(stand, root, sizeof(Inode));

    initInstrucationMap();
    cout << "\n  Welcome to our group including renbo-201830570248 and shixuanli-201830570255  \n\n";
    string pwd = "/";
    string command;
    while(1) {
        cout << "[root ";
        cout << pwd << ']';
        cin >> command;

        if(command == "quit")
            break;
        dealCommand(command, pwd);
    }

    fclose(fp);
}

void initInstrucationMap() {
    instruction["createFile"] = Instr::CREAT_FILE;
    instruction["deleteFile"] = Instr::DELETE_FILE;
    instruction["createDir"] = Instr::CREATE_DIR;
    instruction["deleteDir"] = Instr::DELETE_DIR;
    instruction["changeDir"] = Instr::CHANGE_DIR;
    instruction["dir"] = Instr::DIR;
    
    instruction["sum"] = Instr::SUM;
    instruction["cp"] = Instr::CP;
    instruction["cat"] = Instr::CAT;  
}

void dealCommand(string command, string& pwd) {
    switch (instruction[command])
    {
    case Instr::CREAT_FILE: 
        createFile();
        break;

    case Instr::CREATE_DIR:
        createDir();
        break;
    
    case Instr::DELETE_FILE:
        deleteFile();
        break;

    case Instr::DELETE_DIR:
        deleteFile();//不小心写到一起去了
        break;

    case Instr::CHANGE_DIR:
        changeDir(pwd);
        break;

    case Instr::DIR:
        listDir();
        break;

    case Instr::SUM:
        sum();
        break;

    case Instr::CP:
        cp();
        break;
    
    case Instr::CAT:
        cat();
        break;

    default:
        break;
    }
}
